import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'

Vue.use(VueRouter)
// const originalPush = VueRouter.prototype.push
// VueRouter.prototype.push = function push(location) {
//   return originalPush.call(this, location).catch(err => err)
// }

const routes = [
  {
    path: '/',
    name: 'home',
    component: HomeView,
  },
  {
    path: '/about',
    name: 'about',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path:'/read',
    name:'read',
    component: () => import('../views/ReadView.vue')
  },
  {
    path:'/list',
    name:'list',
    component: () => import('../views/ListView.vue')
  },
  {
    path:'/shelf',
    name:'shelf',
    component: () => import('../views/BookShelfView.vue'),
    beforeEnter: (to, from, next) => {
      let token = window.localStorage.getItem("token");
      if (!token) {
        next("/login");
        return;
      } else {
        next();
      }

    }
  },
  {
    path:'/library',
    name:'library',
    component: () => import('../views/BookLibraryView.vue')
  },
  {
    path:'/rank',
    name:'rank',
    component: () => import('../views/RankView.vue')
  },
  {
    path:'/end',
    name:'end',
    component: () => import('../views/BookEndView.vue')
  },
  {
    path:'/pay',
    name:'pay',
    component: () => import('../views/PayView.vue')
  },
  {
    path:'/user',
    name:'user',
    component: () => import('../views/UserView.vue'),
    beforeEnter: (to, from, next) => {
      let token = window.localStorage.getItem("token");
      if (!token) {
        next("/login");
        return;
      } else {
        next();
      }

    }
  },
  {
    path:'/search',
    name:'search',
    component: () => import('../views/SearchView.vue')
  },
  {
    path:'/login',
    name:'login',
    component: () => import('../views/LoginView.vue')
  },
  {
    path:'/comm',
    name:'comm',
    component: () => import('../views/CommentView.vue')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

export default router
